package org.xqyy.eval.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.xqyy.eval.dao.*;
import org.xqyy.eval.model.*;
import org.xqyy.eval.service.ITestPaperService;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxiaoming on 16-12-25.
 */


@Component
public class TestPaperService implements ITestPaperService {

    @Autowired
    private ITestPaperDao testPaperDao;
    @Autowired
    private TPaperMapper paperMapper;
    @Autowired
    private TDeptmapperPaperMapper tDeptmapperPaperMapper;
    @Autowired
    private TTestPaperMapper testPaperMapper;
    @Autowired
    private TTestPaperDetailMapper testPaperDetailMapper;
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void add(Map<String, Object> params) {
        testPaperDao.add(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void addDetail(Map<String, Object> res) {
        String[] ids = (String[]) res.get("ids");
        String paperId = (String) res.get("paperId");
        if (ids != null && ids.length > 0) {
            for (String questionId : ids) {
                Map<String, Object> params = new HashMap<>();
                params.put("questionId", questionId);
                params.put("paperId", paperId);
                testPaperDao.addDetail(params);
            }
        }
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void deleteDetail(Map<String, Object> res) {
        String[] ids = (String[]) res.get("ids");
        String paperId = (String) res.get("paperId");
        if (ids != null && ids.length > 0) {
            for (String questionId : ids) {
                Map<String, Object> params = new HashMap<>();
                params.put("questionId", questionId);
                params.put("paperId", paperId);
                testPaperDao.deleteDetail(params);
            }
        }
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void modify(Map<String, Object> params) {
        testPaperDao.modify(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void delete(Map<String, Object> params) {
        boolean flag = testPaperDao.checkHasDetailRecord(params);
        if (!flag) {
            testPaperDao.delete(params);
        } else {
            throw new RuntimeException("该问卷已经含有题目，不能删除!");
        }
    }

    @Override
    public Map<String, Object> loadOne(long paperId) {
        Map<String, Object> params = new HashMap();
        params.put("paperId", paperId);
        return testPaperDao.loadOne(params);
    }

    @Override
    public List<Map<String, Object>> load(Map<String, Object> params) {
        return testPaperDao.load(params);
    }

    @Override
    public  List<TTestPaper> selectAllPaper(){
        return testPaperMapper.selectAll();
    }

    /**
     * 待评价科室绑定调查问卷
     * @param deptId 评价科室code
     * @param evalDeptIdList 待评价科室code集合
     * @param paperId 调查问卷Id
     * @return
     */
    @Override
    public  boolean addEvalDeptPaper(String deptId, List<String> evalDeptIdList,Integer paperId){
        for (String evalDeptId : evalDeptIdList) {
            if (!"".equals(evalDeptId) &&  evalDeptId != null){
                TDeptmapperPaper deptmapperPaper = new TDeptmapperPaper();
                deptmapperPaper.setDeptid(deptId);
                deptmapperPaper.setEvaldeptid(evalDeptId);
                tDeptmapperPaperMapper.delete(deptmapperPaper);
                deptmapperPaper.setPapeid(paperId);
                tDeptmapperPaperMapper.insert(deptmapperPaper);
            }
        }
        return true;
    }

    /**
     * 待评价科室绑定调查问卷
     * @param referdeptid  参考部门id
     * @param deptIdList 拷贝到的部门id
     * @return
     */
    public  boolean copyEvalDeptPaper(String referdeptid, List<String> deptIdList){
        TDeptmapperPaper deptmapperPaper = new TDeptmapperPaper();
        deptmapperPaper.setDeptid(referdeptid);
        List<TDeptmapperPaper> tDeptmapperPapers = tDeptmapperPaperMapper.select(deptmapperPaper);
        for (String deptId : deptIdList) {
            if (!"".equals(deptId) && deptId != null){
                delDeptMapperPaper(deptId);
                for (TDeptmapperPaper tDeptmapperPaper : tDeptmapperPapers) {
                    tDeptmapperPaper.setDeptid(deptId);
                    tDeptmapperPaper.setId(null);
                    tDeptmapperPaperMapper.insert(tDeptmapperPaper);
                }
            }
        }
        return true;
    }

    /**
     * 根据科室code删除待评价科室关联调查问卷
     * @param deptId
     * @return
     */
    private  boolean delDeptMapperPaper(String deptId){
        TDeptmapperPaper deptmapperPaper = new TDeptmapperPaper();
        deptmapperPaper.setDeptid(deptId);
        tDeptmapperPaperMapper.delete(deptmapperPaper);
        return true;
    }

    /**
     * 添加满意度调查问卷
     *
     * @param testPaper
     * @return
     */
    @Override
    public Boolean addTestPaper(TTestPaper testPaper, List<TQuestion> questions) {
        testPaper.setType("A");
        testPaperMapper.insert(testPaper);
        for (TQuestion question : questions) {
            TTestPaperDetail tTestPaperDetail = new TTestPaperDetail();
            tTestPaperDetail.setPaperId(testPaper.getPaperId());
            tTestPaperDetail.setQuestionId(Integer.valueOf(String.valueOf(question.getId()) ) );
            testPaperDetailMapper.insert(tTestPaperDetail);
        }
        return true;
    }

    /**
     * 删除满意度调查问卷
     *
     * @param testPaper
     * @return
     */
    @Override
    public Boolean delTestPaper(TTestPaper testPaper) {
        Example example = new Example(TTestPaperDetail.class);
        example.createCriteria().andEqualTo("paperId",testPaper.getPaperId());
        testPaperDetailMapper.deleteByExample(example);
        testPaperMapper.deleteByPrimaryKey(testPaper);
        return true;
    }


    /**
     * 查询满意度满意度调查问卷
     *
     * @param testPaper
     * @return
     */
    @Override
    public TTestPaper findTestPaperById(TTestPaper testPaper) {
        testPaper = testPaperMapper.selectByPrimaryKey(testPaper.getPaperId());
        Example example = new Example(TTestPaperDetail.class);
        example.createCriteria().andEqualTo("paperId",testPaper.getPaperId());
        List<TTestPaperDetail> testPaperDetails = testPaperDetailMapper.selectByExample(example);
        List<TQuestion> questionList = new ArrayList<>();
        for (TTestPaperDetail testPaperDetail : testPaperDetails) {
            TQuestion question = new TQuestion();
            question.setId(Integer.parseInt(String.valueOf(testPaperDetail.getQuestionId()) ) );
            questionList.add(question);
        }
        testPaper.setQuestions(questionList);
        return testPaper;
    }

    /**
     * 修改满意度满意度调查问卷
     *
     * @param testPaper
     * @return
     */
    @Override
    public Boolean updateTestPaper(TTestPaper testPaper) {
        TTestPaper  tempPaper = findTestPaperById(testPaper);
        testPaper.setType(tempPaper.getType());
        Example example = new Example(TTestPaperDetail.class);
        example.createCriteria().andEqualTo("paperId",testPaper.getPaperId());
        testPaperDetailMapper.deleteByExample(example);
        for (TQuestion question : testPaper.getQuestions()) {
            TTestPaperDetail tTestPaperDetail = new TTestPaperDetail();
            tTestPaperDetail.setPaperId(testPaper.getPaperId());
            tTestPaperDetail.setQuestionId(Integer.valueOf(String.valueOf(question.getId()) ) );
            testPaperDetailMapper.insert(tTestPaperDetail);
        }
        testPaperMapper.updateByPrimaryKey(testPaper);
        return true;
    }
}
